/*jshint node:true*/
'use strict';

//创建express，加载依赖模块
var express      = require('express'),
    app          = express(),
    bodyParser   = require('body-parser'),
    compress     = require('compression'),
    cors         = require('cors'),
    favicon      = require('static-favicon'),
    isDev        = app.get('env') === 'development',
    logger       = require('morgan'),
    
    port         = process.env.PORT || 7070,         	//服务端口号,默认为7000
    errorHandler = require('./utils/errorHandler')();   //加载自定义错误处理模块

//express功能模块应用
app.use(compress()); 									// 压缩 response data with gzip
app.use(logger('dev')); 							 	// logger
app.use(bodyParser.urlencoded({extended: true}));    	//body parser, url encoding
app.use(bodyParser.json());						     	//body parser, json
app.use(cors()); 			                            //开启CORS访问
app.use(favicon(__dirname + '/favicon.ico'));		    //开启站点图标
app.use('/', express.static(__dirname + '/static')); 	//支持静态文件,目录static,默认文件为index.html,默认路径为/
app.use(errorHandler.init);                             //HTTP异常处理配置errorHandler.js

//加载HTTP路由定义,routes.index里定义了需要加载哪些内容
require('./routes/routes.index')(app);

//如果是开发模式，则可以通过/test来检查确认
if(isDev){
    app.get('/test', function(req, res, next) {
        console.log(req.body);
        res.send('is development');
    });
}

//创建http服务，在控制台输出日志
app.listen(port, function(){
    console.log('Express server run ok !');
    console.log('port = ' + port);
    console.log('env = '+ app.get('env') +
        '\n__dirname = ' + __dirname  +
        '\nprocess.cwd = ' + process.cwd() );
    console.log('url = http://127.0.0.1:' + port);
    console.log('\nrunning ...');
});